package world0;

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Polygon;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import world0.gui.UserInterface;
import world0.items.Weapons.close.Sword;
import world0.mobs.Quado;
import world0.npc.NpcWaldo;
import world0.util.Position;

public class GameplayState extends BasicGameState
{
	int stateID = -1;
	
	public static final String gameVersion = "0.01";
	public static boolean DEBUG = true;
	
	private Image background;
	private Image background1;
	private Image background2;
	
	private LevelObject ground;
	private LevelObject crate;
	private Mob mob;

	private Player player;
	
	int mouseX;
	int mouseY;
	
	int worldX;
	int worldY;
	
	private UserInterface userInterface;
	
	/* Level object properties*/
	private final int solid = 1;
	private final int cloud = 2;
	private final int slope = 3;
	
	ArrayList<LevelObject> solidList;
	ArrayList<LevelObject> slopeList;
	ArrayList<Mob> mobList;
	ArrayList<Npc> npcList;
	
	Sword sword;
	Sword sword2;
	
	Polygon polygon;
	
	GameplayState (int stateID) {
		this.stateID = stateID;
	}
	
	@Override
	public int getID()  {
		return stateID;
	}

	public void init (GameContainer gc, StateBasedGame sbg) throws SlickException {
		Position.screenHeight = gc.getHeight();
		Position.screenWidth = gc.getWidth();
		
		//MAX OBJECTS 2150!
		/* Level nonsolid */
		background = new Image("textures/levels/world_1/background_back.png");
		background1 = new Image("textures/levels/world_1/background_middle.png");
		background2 = new Image("textures/levels/world_1/background_front.png");
		
		/* Level solid */
		solidList = new ArrayList<LevelObject>();
		slopeList = new ArrayList<LevelObject>();
		
		
		slopeList.add(crate = new LevelObject(101, -64, new Image("textures/levels/world_0/crate_slope_1.png"), slope, 1));
		slopeList.add(crate = new LevelObject(227, -64, new Image("textures/levels/world_0/crate_slope_2.png"), slope, 2));
		solidList.add(crate = new LevelObject(164, -65, new Image("textures/levels/world_0/crate.jpg"), cloud));
		
		solidList.add(crate = new LevelObject(90, -200, new Image("textures/levels/world_0/crate.jpg"), cloud));
		
		solidList.add(crate = new LevelObject(0, -64, new Image("textures/levels/world_0/crate.jpg"), solid));
		solidList.add(crate = new LevelObject(500, -64, new Image("textures/levels/world_0/crate.jpg"), solid));
		
		solidList.add(crate = new LevelObject(0, -128, new Image("textures/levels/world_0/crate.jpg"), solid));
		solidList.add(crate = new LevelObject(500, -128, new Image("textures/levels/world_0/crate.jpg"), solid));
		
		solidList.add(ground = new LevelObject(0, -1, new Image("textures/levels/world_0/ground2.jpg"), solid));
				
		/* Mobs */
		mobList = new ArrayList<Mob>();
		
		Quado quado = new Quado(200, -250, 1);
		
		mobList.add(quado);
		
		/* Npcs */
		npcList = new ArrayList<Npc>();
		
		NpcWaldo waldo = new NpcWaldo(300, -150, 1); // x; y; index;
		waldo.setName("Cpt. Waldo");
		waldo.setLive(100);
		waldo.setEnergy(100, "Mana");
		waldo.setGod(true);
		waldo.setShop(false);
		waldo.setAttitude(true);
		waldo.setRandomWalk(true);
		waldo.setFly(false);
		
		//npcList.add(waldo);
		
		NpcWaldo waldo2 = new NpcWaldo(200, -250, 1); // x; y; index;
		waldo2.setName("Cpt. Waldo");
		waldo2.setLive(9999);
		waldo2.setEnergy(100, "Mana");
		waldo2.setGod(true);
		waldo2.setShop(false);
		waldo2.setAttitude(true);
		waldo2.setRandomWalk(true);
		waldo2.setFly(false);
		waldo2.setRandomWalkRadius(500);
		waldo2.setSpeed(0.3f);
		
		npcList.add(waldo2);
		
		/* Player */
		player = new Player(100, -280, new Image("textures/charakters/spieler1.png"), 1);
		/* init Interface */
		userInterface = new UserInterface(player, gc);
		
		sword = new Sword("Sword", new Image("textures/items/sword.png"));
		sword2 = new Sword("Magic Sword", new Image("textures/items/sword.png"));
		player.giveItem(sword, userInterface);
		player.giveItem(sword2, userInterface);
		
				
	}
	
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {

		Rectangle rectangel = new Rectangle(0, -500, 5000, 1000);
		Rectangle rectangel2 = new Rectangle(0, 500, 5000, 1000);
		g.setColor(Color.cyan);
		g.fill(rectangel);
		g.setColor(Color.green);
		g.fill(rectangel2);
		
		background.draw();
		
		float x = player.getX();
		float y = player.getY();
		background1.draw(((x / 35)*(-1)), (30 - y / 60));
		background2.draw(((x / 18)*(-1)), (25 - y /20));

		
		g.translate(-player.getX() + gc.getWidth()/2, -player.getY() + gc.getHeight()/2);
		for (int i = 0; i < solidList.size(); i++) {
			solidList.get(i).draw(g);
		}
		
		for (int i = 0; i < slopeList.size(); i++) {
			slopeList.get(i).draw(g);
		}
		
		for (int i = 0; i < mobList.size(); i++) {
			mobList.get(i).draw(g);
		}
		
		for (int i = 0; i < npcList.size(); i++) {
			npcList.get(i).draw(g);
		}

		ground.draw(g);
		
		g.setColor(Color.blue);
		
		player.draw(g);
		
		g.resetTransform();

		
		userInterface.draw(g, gc);
	}
	
	public void update (GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
	{
		Input input = gc.getInput();

		mouseX = input.getMouseX();
		mouseY = input.getMouseY();
		
		int cameraX = (int)player.getX() - (gc.getWidth() / 2);
		worldX = cameraX + mouseX;
		int cameraY = (int)player.getY() - (gc.getHeight() / 2);
		worldY = cameraY + mouseY;
		
		if (input.isMousePressed(Input.MOUSE_RIGHT_BUTTON) && !userInterface.isHit()) {
			solidList.add(crate = new LevelObject(worldX, worldY - 64, new Image("textures/levels/world_0/crate.jpg"), solid));
		}
		
		if(input.isControlPressed(9)) {
			solidList.add(crate = new LevelObject(player.getX() - 16, player.getY() + 32, new Image("textures/levels/world_0/crate.jpg"), solid));
		}
		if(input.isControlPressed(11) || input.isMousePressed(Input.MOUSE_LEFT_BUTTON) && !userInterface.isHit()) {
			mobList.add(new Quado(player.getX() - 16, player.getY(), 2));
		}
		
		player.update(delta, input, solidList, slopeList);
		
		for (int i = 0; i < mobList.size(); i++) {
			mobList.get(i).update(delta, solidList, slopeList, mobList, player);
		}

		for (int i = 0; i < npcList.size(); i++) {
			npcList.get(i).update(delta, solidList, slopeList, mobList, player);
		}

		userInterface.update(gc, delta);
		
		if(input.isKeyPressed(Input.KEY_ESCAPE))
		{
			gc.setMouseGrabbed(false);
			sbg.enterState(World0Game.MAINMENUSTATE);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {e.printStackTrace();}
		}
	}
}
